//
//  PayStyleUtil.m
//  customer
//
//  Created by Holothurian on 2018/11/11.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "PayStyleUtil.h"

@interface PayStyleUtil ()

/// 微信支付回调
@property (nonatomic, copy) void(^wxPayDone)(NSString* errorMsg);


@end

@implementation PayStyleUtil

static PayStyleUtil* kInstance;
+(PayStyleUtil*)getInstance{
    if (!kInstance) {
        kInstance = [[PayStyleUtil alloc] init];
    }
    return kInstance;
}

/*
 商家号：1514583411
 key：us37kna3gyfFlz1zpXWResh2TK2F3fEt
 AppID: wxb6577df0ed8afe66
 AppSecret：ab2f88418d853c4dabbf887cb6c1ff1e
 */

#pragma mark - ******** 微信 ********
/// 微信支付
+ (void)doWxpay:(PayOrderModel *)payParam payDone:(void(^)(NSString* errorMsg))payDone;
{
    [WXApi registerApp:WXOpenID];
    [self getInstance].wxPayDone = payDone;
    NSLog(@">>%@",payParam);
    PayReq* wxPay = [[PayReq alloc] init];
    wxPay.openID = payParam.appid;
    wxPay.partnerId = payParam.partnerid;
    wxPay.package = payParam.package;
    wxPay.prepayId = payParam.prepayid;
    wxPay.nonceStr = payParam.noncestr;
    wxPay.timeStamp =  [payParam.timestamp intValue];
    wxPay.sign = payParam.sign;
    
    BOOL isSuccess = [WXApi sendReq:wxPay];
    if (!isSuccess) {
        SHOW_ALERT(nil, @"调用微信支付失败", nil, @"确定", 68);
    }
}

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        id errorMsg = nil;
        switch (response.errCode) {
            case WXSuccess:
                break;
            case WXErrCodeCommon:
                errorMsg = @"未知错误";
                break;
            case WXErrCodeUserCancel:
                errorMsg = @"取消支付";
                break;
            case WXErrCodeSentFail:
                errorMsg = @"发送失败";
                break;
            case WXErrCodeAuthDeny:
                errorMsg = @"未知错误";
                break;
            case WXErrCodeUnsupport:
                errorMsg = @"未知错误";
                break;
            default:
                break;
        }
        self.wxPayDone(errorMsg);
    }
    
}

+ (void)doneWxpay:(BaseResp*)resp;
{
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp *)resp;
        id errorMsg = nil;
        switch (response.errCode) {
            case WXSuccess:
                break;
            case WXErrCodeCommon:
                errorMsg = @"未知错误";
                break;
            case WXErrCodeUserCancel:
                errorMsg = @"取消支付";
                break;
            case WXErrCodeSentFail:
                errorMsg = @"发送失败";
                break;
            case WXErrCodeAuthDeny:
                errorMsg = @"未知错误";
                break;
            case WXErrCodeUnsupport:
                errorMsg = @"未知错误";
                break;
            default:
                break;
        }
        [self getInstance].wxPayDone(errorMsg);
    }
}

@end
